﻿#region Copyright Statement
//  ------------------------------------------------------------------------------
//  
//    This file is part of The Streambolics Library.
//    Copyright © 2005-2010, Stephan Leclercq & Streambolics
//    All rights reserved.
//  
//    The Streambolics Library is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//  
//    The Streambolics Library is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//  
//    You should have received a copy of the GNU General Public License
//    along with Streambolics; if not, write to the Free Software
//    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//  
//  ------------------------------------------------------------------------------
#endregion

using System;
using System.IO;

namespace Myxin
{
    public static class StringExtensions
    {
        public static string AfterFirst (this string s, string aTag)
        {
            int p = s.IndexOf (aTag);
            if (p < 0)
            {
                return null;
            }
            else
            {
                return s.Substring (p + aTag.Length);
            }
        }
        public static string BeforeFirst (this string s, string aTag)
        {
            int p = s.IndexOf (aTag);
            if (p < 0)
            {
                return null;
            }
            else
            {
                return s.Substring (0, p);
            }
        }
        public static string BetweenFirst (this string s, string aStartTag, string aEndTag)
        {
            string ss = s.AfterFirst (aStartTag);
            if (ss == null)
            {
                return null;
            }
            else
            {
                return ss.BeforeFirst (aEndTag);
            }
        }
    }

    class Program
    {
        static void Execute ()
        {
            Console.WriteLine ("Myxin 0.99 - (c) Streambolics");

            DirectoryInfo d = new DirectoryInfo (Directory.GetCurrentDirectory ());

            if (d.FullName.Contains ("\\bin\\"))
            {
                d = d.Parent.Parent;
                Directory.SetCurrentDirectory (d.FullName);
            }

            Console.WriteLine ("  Directory: " + d.FullName);
            foreach (FileInfo f in d.GetFiles ())
            {
                string fn = f.Name;
                if (fn.EndsWith (".cs"))
                {
                    string BaseClass = fn.BeforeFirst (".Mixin.");
                    string MixinSource = fn.BetweenFirst (".Mixin.", ".cs");

                    if (!String.IsNullOrEmpty (BaseClass) && !String.IsNullOrEmpty (MixinSource))
                    {
                        Console.WriteLine ("  Mixing: " + fn);
                        MixinSource += "Mixin";
                        string IncludeFile = MixinSource + ".cs";
                        if (!File.Exists (IncludeFile))
                        {
                            throw new Exception ("File not found " + IncludeFile);
                        }
                        string[] s = File.ReadAllLines (IncludeFile);
                        for (int i = 0; i < s.Length; i++)
                        {
                            s[i] = s[i].Replace (MixinSource, BaseClass);

                        }
                        File.WriteAllLines (fn, s);
                    }

                }
            }
        }

        static void Main (string[] args)
        {
            bool Silent = args.Length != 0;
            TextWriter StdOut = Console.Out;
            TextWriter CurOut = Console.Out;

            // TODO : change CurOut here

            try
            {
                Console.SetOut (CurOut);
                try
                {
                    Execute ();
                }
                finally
                {
                    Console.SetOut (StdOut);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine ("**** " + ex.Message);
                Silent = false;
                
            }

            if (!Silent)
            {
                Console.SetOut (StdOut);
                Console.WriteLine ("Press ENTER to exit");
                Console.ReadLine ();
            }
        }
    }
}
